{% extends 'configmanager/__base__.html' %}
{% block title %}发布申请详情 | {% endblock %}
{% block search_display %}none{% endblock %}
{% block content %}

    <ol class="breadcrumb">
        <li><a href="{% url 'configmanager:applylist' %}">发布申请列表</a></li>
        <li class="active">修改发布申请</li>
    </ol><br>

    {% load systemmanager %}

    <script type="text/html" id="item-template">
        <tr id="deployitem-set-__prefix__">
            <td>{{ deployitem_form.empty_form.deployorderby }}</td>
            <td>{{ deployitem_form.empty_form.jenkinsversion }}</td>
            <td>{{ deployitem_form.empty_form.deploysite }}</td>
            <td>{{ deployitem_form.empty_form.type }}</td>
            <td>{{ deployitem_form.empty_form.DELETE }}</td>
            <td style="display:none">{{ deployitem_form.empty_form.id }}</td>
            <td style="display:none">{{ deployitem_form.empty_form.applyproject }}</td>
            {# <!-- or for crispy forms --> {% crispy deployitem_form.empty_form item_forms.form.helper %} #}
        </tr>
    </script>

    <script>
        $(document).ready(function () {
            $('.add-item').click(function (ev) {
                ev.preventDefault();
                var count = $('#id-deployitem-tbody').children().length;
                var tmplMarkup = $('#item-template').html();
                var compiledTmpl = tmplMarkup.replace(/__prefix__/g, count);
                $('tbody#id-deployitem-tbody').append(compiledTmpl);

                //update form count
                $('#id_item_items-TOTAL_FORMS').attr('value', count + 1);
            });
        });


    </script>

    <script type="text/javascript">
        var pageObject = {
            //展开操作日志
            showOperateLog: function (obj) {
                if ($.trim($(obj).text()) == "展开操作日志") {
                    $("#divOperateLog").show();
                    $(obj).text("收起操作日志");
                }
                else {
                    $("#divOperateLog").hide();
                    $(obj).text("展开操作日志");
                }
            }
        }

    </script>

    <div id="whole" class="container-fluid table-responsive">
        <form action="{% url 'configmanager:applystatuschange' apply.id %}" method="post">
            {% csrf_token %}
            {{ apply_form.management_form }}
            {{ deployitem_form.management_form }}
            <input type="hidden" name="deployitem_set-TOTAL_FORMS" id="id_item_items-TOTAL_FORMS"
                   value="{{ deployitem_form.total_form_count }}">
            <table class="table table-hover table-condensed">
                <tbody>
                <tr>
                    <th>申请编号</th>
                    <td>{{ apply.applyproject }}</td>
                </tr>
                <tr>
                    <th>期望发布时间</th>
                    {% if apply.status == "WC" %}
                        <td>
                            <input type="date" name="wishdeploy_time" id="id_wishdeploy_time" size="10"
                                   value="{{ apply.wishdeploy_time|date:'Y-m-d' }}"/>
                        </td>
                    {% else %}
                        <td>{{ apply.wishdeploy_time }}</td>
                    {% endif %}
                </tr>
                <tr>
                    <th>选择发布站点</th>
                    <td>
                        <table class="table table-striped table-hover table-condensed table-bordered"
                               id="table_id_deployitem">
                            {% if apply.status == 'WC' %}
                                <thead>
                                <tr>
                                    {% for form in deployitem_form %}
                                        {% if forloop.first %}
                                            {% for field in form %}
                                                {% if field.label != "Id" and field.label != "Applyproject" %}
                                                    <th>{{ field.label }}</th>
                                                {% endif %}
                                            {% endfor %}
                                        {% endif %}
                                    {% endfor %}
                                </tr>
                                </thead>
                                <tbody id="id-deployitem-tbody">
                                {% for form in deployitem_form %}
                                    <tr id="deployitem-set-{{ forloop.counter0 }}">
                                        <td>{{ form.deployorderby }}</td>
                                        <td>{{ form.jenkinsversion }}</td>
                                        <td>{{ form.deploysite }}</td>
                                        <td>{{ form.type }}</td>
                                        <td>{{ form.DELETE }}</td>
                                        <td style="display:none">{{ form.id }}</td>
                                        <td style="display:none"
                                        {{ form.applyproject }}
                                        </td>
                                        {# <!-- or for crispy forms --> {% crispy deployitem_form %} #}
                                    </tr>
                                {% endfor %}
                                </tbody>
                                <tfoot>
                                <tr class="add-row">
                                    <td colspan="7"><a href="#" class="add-item">添加另一个 发布站点</a></td>
                                </tr>
                                </tfoot>
                            {% else %}
                                <thead>
                                <tr>
                                    <th>发布顺序</th>
                                    <th>Jenkins版本号</th>
                                    <th>代码类型</th>
                                    <th>发布站点</th>
                                    <th>所属ECS</th>
                                    <th>发布状态</th>
                                </tr>
                                </thead>
                                <tbody>
                                <tr>
                                    {% for deployitem in apply.deployitem_set.all %}
                                        <td>{{ deployitem.deployorderby }}</td>
                                        <td>{{ deployitem.jenkinsversion }}</td>
                                        <td>
                                            {% if deployitem.type == "T" %}主干{% else %}分支{% endif %}
                                        </td>
                                        <td>{{ deployitem.deploysite }}</td>
                                        <td>
                                            {% for deployecs in deployitem.deploysite.get_ECSlists_list %}
                                                <p>{{ deployecs }}</p>
                                            {% endfor %}
                                        </td>
                                        <td>
                                            {% for deploystatus in deployitem.deploysite.deployecs_set.all %}
                                                {% if deploystatus.applyproject.id == apply.id %}
                                                    <p>{% if deploystatus.ECSdeploystatus == "Y" %}
                                                        <span class="text-success"><strong>已发布</strong></span>{% else %}
                                                        <span class="text-danger"><strong>待发布</strong></span>{% endif %}
                                                    </p>
                                                {% endif %}
                                            {% endfor %}
                                        </td>
                                    {% endfor %}
                                </tr>
                                </tbody>
                            {% endif %}
                        </table>

                    </td>
                </tr>
                <tr>
                    <th valign="top">配置修改说明</th>
                    <td>
                        {% if apply.status == "WC" %}
                            {{ apply_form.confamendexplain }}
                        {% else %}
                            <textarea readonly="readonly" name="confamendexplain" rows="20" cols="80"
                                      id="id_confamendexplain">{{ apply.confamendexplain }}</textarea>
                        {% endif %}
                    </td>
                </tr>
                <tr>
                    <th valign="top">备注事项</th>
                    <td>
                        {% if apply.status == "WC" %}
                            {{ apply_form.remarkexplain }}
                        {% else %}
                            <textarea readonly="readonly" name="confamendexplain" rows="20" cols="80"
                                      id="id_remarkexplain">{{ apply.remarkexplain }}</textarea>
                        {% endif %}
                    </td>
                </tr>
                <tr>
                    <th>状态</th>
                    <td name="deploystatus">
                        {% if apply.status == "WC" %}
                            <span>待提交</span>
                        {% elif apply.status == "WD" %}
                            <span>待发布</span>
                        {% elif apply.status == "C" %}
                            <span>已取消</span>
                        {% elif apply.status == "D" %}
                            <span>已发布</span>
                        {% elif apply.status == "DA" %}
                            <span>研发经理审批中</span>
                        {% elif apply.status == "OA" %}
                            <span>运维经理审批中</span>
                        {% elif apply.status == "EA" %}
                            <span>运维工程师审批中</span>
                        {% elif apply.status == "TA" %}
                            <span>测试经理审批中</span>
                        {% elif apply.status == "TDA" %}
                            <span>技术总监审批中</span>
                        {% endif %}
                    </td>
                </tr>
                <tr>
                    <th>申请人</th>
                    <td>
                        {{ apply.apply_user }}
                    </td>
                </tr>
                <tr>
                    <th>申请时间</th>
                    <td>
                        {{ apply.apply_time }}
                    </td>
                </tr>
                <tr>
                    <th><a href="javascript:void(0)" onclick="pageObject.showOperateLog(this);">展开操作日志</a></th>
                    <td>
                        <div style="display:none;" id="divOperateLog" class="container-fluid table-responsive">
                            <table class="table table-striped table-hover table-condensed">
                                <thead>
                                <tr>
                                    <th>操作类型</th>
                                    <th>操作人</th>
                                    <th>操作时间</th>
                                </tr>
                                </thead>
                                <tbody>
                                {% for log in apply.applyoperatelog_set.all %}
                                    <tr>
                                        <td>{{ log.type }}</td>
                                        <td>{{ log.OperatorName }}</td>
                                        <td>{{ log.OperationTime }}</td>
                                    </tr>
                                {% endfor %}
                                </tbody>
                            </table>
                        </div>
                    </td>
                </tr>
                </tbody>
            </table>

            <input class="btn btn-primary" type="{% if apply.status == 'WC' %}submit{% else %}hidden{% endif %}"
                   name="apply-save" value="保存"/>
            <input class="btn btn-success" type="{% if apply.status == 'WC' %}submit{% else %}hidden{% endif %}"
                   name="apply-commit" value="保存并提交"/>
            <input class="btn btn-warning" type="{% if apply.status == 'WC' %}submit{% else %}hidden{% endif %}"
                   name="apply-cancel" value="取消"/>

            {% if request.user|has_group:"研发经理" or request.user|has_group:"admin" or request.user.is_superuser %}
                <input class="btn btn-primary" type="{% if apply.status == 'DA' %}submit{% else %}hidden{% endif %}"
                       name="dev-approval" value="研发经理审核通过"/>
                <input class="btn btn-warning" type="{% if apply.status == 'DA' %}submit{% else %}hidden{% endif %}"
                       name="dev-unapproval" value="研发经理审核不通过"/>
            {% endif %}

            {% if request.user|has_group:"测试经理" or request.user|has_group:"admin" or request.user.is_superuser %}
                <input class="btn btn-primary" type="{% if apply.status == 'TA' %}submit{% else %}hidden{% endif %}"
                       name="test-approval" value="测试经理审核通过"/>
                <input class="btn btn-warning" type="{% if apply.status == 'TA' %}submit{% else %}hidden{% endif %}"
                       name="test-unapproval" value="测试经理审核不通过"/>
            {% endif %}

            {% if request.user|has_group:"运维工程师" or request.user|has_group:"admin" or request.user.is_superuser %}
                <input class="btn btn-primary" type="{% if apply.status == 'EA' %}submit{% else %}hidden{% endif %}"
                       name="EA-approval" value="运维工程师审核通过"/>
                <input class="btn btn-warning" type="{% if apply.status == 'EA' %}submit{% else %}hidden{% endif %}"
                       name="EA-unapproval" value="运维工程师审核不通过"/>
            {% endif %}

            {% if request.user|has_group:"运维经理" or request.user|has_group:"admin" or request.user.is_superuser %}
                <input class="btn btn-primary" type="{% if apply.status == 'OA' %}submit{% else %}hidden{% endif %}"
                       name="OA-approval" value="运维经理审核通过"/>
                <input class="btn btn-warning" type="{% if apply.status == 'OA' %}submit{% else %}hidden{% endif %}"
                       name="OA-unapproval" value="运维经理审核不通过"/>
            {% endif %}

            {% if request.user|has_group:"技术总监" or request.user|has_group:"admin" or request.user.is_superuser %}
                <input class="btn btn-primary" type="{% if apply.status == 'TDA' %}submit{% else %}hidden{% endif %}"
                       name="TDA-approval" value="技术总监审核通过"/>
                <input class="btn btn-warning" type="{% if apply.status == 'TDA' %}submit{% else %}hidden{% endif %}"
                       name="TDA-unapproval" value="技术总监审核不通过"/>
            {% endif %}

            {% if request.user|has_group:"运维工程师" or request.user|has_group:"admin" or request.user.is_superuser %}
                <input class="btn btn-success" type="{% if apply.status == 'WD' %}submit{% else %}hidden{% endif %}"
                       name="deploy-finish" value="已发布"/>
                <input class="btn btn-info" type="{% if apply.status == 'WD' %}submit{% else %}hidden{% endif %}"
                       name="goto-deploy" value="现在去发布"/>
            {% endif %}

            <input class="btn btn-default" type="submit" name="goback" value="返回" onclick='location.href=("/apply")'/>
        </form>
    </div>

{% endblock %}
